
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>JSX 什么鬼(一起来写一个 JSX 渲染器) · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Redux:从0创建Store.html" />
    
    
    <link rel="prev" href="创建你自己的vuejs/创建你自己的vuejs.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    前端框架
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="创建你自己的react/创建你自己的react.html">
            
                <a href="创建你自己的react/创建你自己的react.html">
            
                    
                    创建你自己的 react
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="创建你自己的vuejs/创建你自己的vuejs.html">
            
                <a href="创建你自己的vuejs/创建你自己的vuejs.html">
            
                    
                    创建你自己的 vuejs
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.3" data-path="JSXWTH.html">
            
                <a href="JSXWTH.html">
            
                    
                    JSX 什么鬼(一起来写一个 JSX 渲染器)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="Redux:从0创建Store.html">
            
                <a href="Redux:从0创建Store.html">
            
                    
                    Redux:从 0 创建 Store
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="我们来写个redux.html">
            
                <a href="我们来写个redux.html">
            
                    
                    我们来写个 redux
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >JSX 什么鬼(一起来写一个 JSX 渲染器)</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>JSX &#x5B9E;&#x9645;&#x4E0A;&#x5F88;&#x7B80;&#x5355;&#xFF1A;&#x8BFB;&#x5B8C;&#x8FD9;&#x7BC7;&#x6587;&#x7AE0;&#xFF0C;&#x4F60;&#x5C31;&#x4F1A;&#x5B8C;&#x5168;&#x4E86;&#x89E3;&#x8FD9;&#x4E2A;&#x53EF;&#x9009;&#x62E9;&#x7684;&#x6A21;&#x7248;&#x5F15;&#x64CE;</p>
<p>&#x526F;&#x6807;&#x9898;&#xFF1A;&#x201C;&#x548C; JSX &#x5171;&#x5904;&#x201D;</p>
<h2 id="&#x6CE8;&#x89E3;">&#x6CE8;&#x89E3;</h2>
<p>&#x4F60;&#x5728;&#x6BCF;&#x4E2A;&#x6587;&#x4EF6;&#x548C;&#x6BCF;&#x4E2A;&#x51FD;&#x6570;&#x91CC;&#x5B9A;&#x4E49;&#x8FD9;&#x4E2A;&#xFF0C;&#x544A;&#x8BC9;&#x8F6C;&#x8BD1;&#x5668;&#xFF08;&#x5982;&#xFF1A;Babel&#xFF09;&#x6BCF;&#x4E2A;&#x8282;&#x70B9;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x9636;&#x6BB5;&#x9700;&#x8981;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x540D;&#x3002;</p>
<p>&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x91CC;&#xFF0C;&#x6211;&#x4EEC;&#x79F0;&#x4E4B;&#x4E3A;&#x201C;&#x5BF9;&#x6BCF;&#x4E2A;&#x8282;&#x70B9;&#xFF0C;&#x63D2;&#x5165; h()&#x51FD;&#x6570;&#x7684;&#x8C03;&#x7528;&#x201D;</p>
<blockquote>
<p>/*<em>@jsx h</em>/</p>
</blockquote>
<h2 id="&#x8F6C;&#x8BD1;">&#x8F6C;&#x8BD1;</h2>
<p>&#x5982;&#x679C;&#x4F60;&#x8FD8;&#x6CA1;&#x6709;&#x4F7F;&#x7528;&#x8FC7;&#x8F6C;&#x8BD1;&#x5668;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x5C1D;&#x8BD5;&#x4F7F;&#x7528;&#x3002;&#x56E0;&#x4E3A;&#x7528; es6/ES2015 &#x5199;&#xFF0C;&#x8C03;&#x8BD5;&#xFF0C;&#x6D4B;&#x8BD5;&#x6216;&#x8FD0;&#x884C; js &#x90FD;&#x66F4;&#x52A0;&#x6709;&#x6548;&#x7387;&#x3002;&#x5176;&#x4E2D;<a href="https://babeljs.io" target="_blank">Babel</a>&#x662F;&#x6700;&#x6D41;&#x884C;&#x7684;&#xFF0C;&#x4E5F;&#x662F;&#x6700;&#x88AB;&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x7684;&#x3002;&#x6211;&#x4EEC;&#x5047;&#x8BBE;&#x4F60;&#x4F7F;&#x7528;&#x4E86; babel</p>
<p>&#x5982;&#x4ECA; babel &#x4E0D;&#x4EC5;&#x63D0;&#x4F9B;&#x8F6C;&#x6362;&#x4F60;&#x7684; ES6/ES7+&#x8BED;&#x6CD5;&#x652F;&#x6301;&#xFF0C;&#x5E76;&#x4E14;&#x63D0;&#x4F9B;&#x76F4;&#x63A5;&#x5F00;&#x7BB1;&#x5373;&#x7528;&#xFF0C;&#x8F6C;&#x6362; JSX &#x7684;&#x652F;&#x6301;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x8FD9;&#x79CD;&#x7279;&#x6027;&#x3002;
&#x6211;&#x4EEC;&#x5148;&#x6765;&#x770B;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;
&#x6709; jsx &#x4E4B;&#x524D;&#xFF08;&#x4F60;&#x600E;&#x4E48;&#x5199;&#x4EE3;&#x7801;&#xFF09;</p>
<pre><code>/** @jsx h */
let foo = &lt;div id=&quot;foo&quot;&gt;Hello!&lt;/div&gt;;
</code></pre><p>&#x6709; jsx &#x540E;&#xFF08;&#x4F60;&#x8FD0;&#x884C;&#x7684;&#x4EE3;&#x7801;&#xFF09;</p>
<p><code>var foo = h(&apos;div&apos;, {id:&quot;foo&quot;}, &apos;Hello!&apos;);</code></p>
<p>&#x4F60;&#x53EF;&#x80FD;&#x770B;&#x5230;&#x7B2C;&#x4E8C;&#x6BB5;&#x4EE3;&#x7801;&#xFF0C;&#x89C9;&#x5F97;&#x7528;&#x51FD;&#x6570;&#x6765;&#x521B;&#x5EFA; UI &#x4E5F;&#x4E0D;&#x9519;</p>
<p>&#x8FD9;&#x5C31;&#x662F;&#x6211;&#x4E3A;&#x4EC0;&#x4E48;&#x4ECE; JSX &#x8BB2;&#x8D77;&#xFF1A;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x8FD9;&#x4E2A;&#xFF0C;&#x4F60;&#x624B;&#x52A8;&#x5199;&#x51FA;&#x6765;&#x4ECD;&#x7136;&#x5F88;&#x7B80;&#x5355;</p>
<blockquote>
<p><strong>JSX &#x53EA;&#x662F;&#x4E00;&#x79CD;&#x5DF2;&#x7ECF;&#x5F88;&#x4F18;&#x96C5;&#x8BED;&#x6CD5;&#x7684;&#x8BED;&#x6CD5;&#x7CD6;</strong></p>
</blockquote>
<p>&#x6709;&#x4EBA;&#x751A;&#x81F3;&#x6574;&#x4E2A;&#x9879;&#x76EE;&#x7528;&#x5B83;<a href="https://github.com/hyperhype/hyperscript" target="_blank">hypescript</a></p>
<h2 id="&#x6211;&#x4EEC;&#x6765;&#x5199;&#x4E2A;-jsx-&#x6E32;&#x67D3;&#x5668;">&#x6211;&#x4EEC;&#x6765;&#x5199;&#x4E2A; jsx &#x6E32;&#x67D3;&#x5668;</h2>
<p>&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x5B9A;&#x4E49;&#x4E0B;&#x6211;&#x4EEC;&#x8F6C;&#x6362;&#x7684;&#x4EE3;&#x7801;&#x8C03;&#x7528;&#x7684; h()&#x51FD;&#x6570;&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x628A;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x547D;&#x540D;&#x4E3A;&#x4EFB;&#x4F55;&#x540D;&#x5B57;&#xFF0C;&#x6211;&#x4E4B;&#x6240;&#x4EE5;&#x7528; h(),&#x662F;&#x56E0;&#x4E3A;&#x5728;<a href="https://github.com/hyperhype/hyperscript" target="_blank">hypescript</a>&#x91CC;&#x8FD9;&#x79CD;&#x7C7B;&#x578B;&#x7684;&#x2018;build&#x2019;&#x51FD;&#x6570;&#x5C31;&#x662F;&#x8FD9;&#x4E48;&#x79F0;&#x547C;&#x7684;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">h</span>(<span class="hljs-params">nodeName, attributes, ...args</span>) </span>{
  <span class="hljs-keyword">let</span> children = args.length ? [].concat(...args) : <span class="hljs-literal">null</span>
  <span class="hljs-keyword">return</span> { nodeName, attributes, children }
}
</code></pre>
<p>&#x597D;&#x4E86;&#xFF0C;&#x5C31;&#x662F;&#x8FD9;&#x4E48;&#x7B80;&#x5355;
&#x4E0D;&#x719F;&#x6089; ES6/ES2005&#xFF1F;</p>
<p>1.&#x53C2;&#x6570;&#x4E2D;&#x7684;&apos;...&apos;&#x662F;&#x5269;&#x4F59;&#x53C2;&#x6570;,&#x8BE5;&#x64CD;&#x4F5C;&#x7B26;&#x4F1A;&#x6536;&#x96C6;&#x5269;&#x4F59;&#x7684;&#x53C2;&#x6570;&#x5230;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x91CC;
2.concat(...args)&#x662F; spread &#x64CD;&#x4F5C;&#x7B26;&#xFF1A;&#x8FD9;&#x4E2A;&#x64CD;&#x4F5C;&#x7B26;&#x4F1A;&#x628A;&#x521A;&#x521A;&#x7684;&#x53C2;&#x6570;&#x6570;&#x7EC4;&#x5C55;&#x5F00;&#x5230; arguments &#x91CC;&#xFF0C;&#x518D;&#x4F20;&#x7ED9; concat()&#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x91CC;&#x7528; concat()&#x662F;&#x4E3A;&#x4E86;&#x5408;&#x5E76;&#x6240;&#x6709;&#x5B50;&#x8282;&#x70B9;&#x7684;&#x5D4C;&#x5957;&#x6570;&#x7EC4;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x901A;&#x8FC7; h()&#x65B9;&#x6CD5;&#x8F93;&#x51FA;&#x4E00;&#x4E2A;&#x5D4C;&#x5957;&#x7684; JSON &#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x4E2A;&#x2018;&#x6811;&#x72B6;&#x2019;&#x5BF9;&#x8C61;&#x5C31;&#x50CF;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-javascript">{
  nodeName: <span class="hljs-string">&quot;div&quot;</span>,
  attributes: {
    <span class="hljs-string">&quot;id&quot;</span>: <span class="hljs-string">&quot;foo&quot;</span>
  },
  children: [<span class="hljs-string">&quot;Hello!&quot;</span>]
}
</code></pre>
<p>&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x63A5;&#x53D7;&#x8FD9;&#x6837;&#x7684;&#x53C2;&#x6570;&#x683C;&#x5F0F;&#xFF0C;&#x5E76;&#x8F93;&#x51FA;&#x771F;&#x5B9E;&#x7684; dom &#x8282;&#x70B9;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">render</span>(<span class="hljs-params">vnode</span>) </span>{
  <span class="hljs-comment">// Strings just convert to #text Nodes:</span>
  <span class="hljs-keyword">if</span> (vnode.split) <span class="hljs-keyword">return</span> <span class="hljs-built_in">document</span>.createTextNode(vnode)

  <span class="hljs-comment">// create a DOM element with the nodeName of our VDOM element:</span>
  <span class="hljs-keyword">let</span> n = <span class="hljs-built_in">document</span>.createElement(vnode.nodeName)

  <span class="hljs-comment">// copy attributes onto the new node:</span>
  <span class="hljs-keyword">let</span> a = vnode.attributes || {}
  <span class="hljs-built_in">Object</span>.keys(a).forEach((k) =&gt; n.setAttribute(k, a[k]))

  <span class="hljs-comment">// render (build) and then append child nodes:</span>
  ;(vnode.children || []).forEach((c) =&gt; n.appendChild(render(c)))

  <span class="hljs-keyword">return</span> n
}
</code></pre>
<p>&#x7406;&#x89E3;&#x8FD9;&#x4E2A;&#x5F88;&#x5BB9;&#x6613;&#x3002;
&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x628A;&#x2018;&#x865A;&#x62DF; DOM&#x2019;&#x8BA4;&#x4E3A;&#x662F;&#x5982;&#x4F55;&#x6784;&#x5EFA; DOM &#x7ED3;&#x6784;&#x7684;&#x4E00;&#x79CD;&#x7B80;&#x5355;&#x914D;&#x7F6E;&#x3002;</p>
<blockquote>
<p>&#x865A;&#x62DF; dom &#x7684;&#x4F18;&#x52BF;&#x662F;&#x5B83;&#x5341;&#x5206;&#x8F7B;&#x91CF;&#x3002;&#x8F7B;&#x91CF;&#x5BF9;&#x8C61;&#x5F15;&#x7528;&#x5176;&#x4ED6;  &#x8F7B;&#x91CF;&#x5BF9;&#x8C61;&#x3002; &#x975E;&#x5E38;&#x5BB9;&#x6613;&#x4F18;&#x5316;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7ED3;&#x6784;&#x3002;&#x8FD9;&#x4E5F;&#x8868;&#x793A;&#x5B83;&#x6CA1;&#x6709;&#x7ED1;&#x5B9A;&#x4EFB;&#x4F55;&#x6E32;&#x67D3;&#x903B;&#x8F91;&#x548C;&#x5F88;&#x6162;&#x7684; DOM &#x65B9;&#x6CD5;&#x3002;</p>
</blockquote>
<h2 id="&#x4F7F;&#x7528;-jsx">&#x4F7F;&#x7528; JSX</h2>
<p>&#x73B0;&#x5728;&#x77E5;&#x9053; JSX &#x88AB;  &#x8F6C;&#x6362;&#x6210;&#x4E86;&#x5BF9; h() &#x7684;&#x8C03;&#x7528;&#x3002;&#x8FD9;&#x4E9B;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x201C;&#x865A;&#x62DF;&#x201D;DOM &#x6811;&#x3002;
&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; render()&#x51FD;&#x6570;&#x53BB;&#x521B;&#x5EFA;&#x5339;&#x914D;&#x7684;&#x201C;&#x771F;&#x5B9E;&#x201D;DOM &#x6811;&#x3002;</p>
<p>&#x5C31;&#x50CF;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// JSX -&gt; VDOM:</span>
<span class="hljs-keyword">let</span> vdom = <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&apos;foo&apos;</span>&gt;</span>Hello!<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>

<span class="hljs-comment">// VDOM -&gt; DOM:</span>
<span class="hljs-keyword">let</span> dom = render(vdom)

<span class="hljs-comment">// add the tree to &lt;body&gt;:</span>
<span class="hljs-built_in">document</span>.body.appendChild(dom)
</code></pre>
<p>&#x5C40;&#x90E8;&#x6A21;&#x7248;&#xFF0C;&#x8FED;&#x4EE3;&#x548C;&#x903B;&#x8F91;&#xFF1A;&#x6CA1;&#x6709;&#x65B0;&#x8BED;&#x6CD5;</p>
<blockquote>
<p>&#x533A;&#x522B;&#x4E8E;&#x6A21;&#x7248;&#x8BED;&#x8A00;&#x5F15;&#x5165;&#x7684;&#x6709;&#x9650;&#x6982;&#x5FF5;&#x548C;&#x5C40;&#x9650;&#xFF0C;&#x6211;&#x4EEC;&#x6709; javascript &#x6240;&#x62E5;&#x6709;&#x7684;&#x4E00;&#x5207;&#x80FD;&#x529B;</p>
</blockquote>
<p>&apos;&#x5C40;&#x90E8;&#x6A21;&#x7248;&apos;&#x662F;&#x7531;&#x65E0;&#x903B;&#x8F91;&#xFF0F;&#x5C11;&#x903B;&#x8F91;&#x7684;&#x6A21;&#x7248;&#x5F15;&#x64CE;&#x4E3A;&#x4E86;&#x5728;&#x4E0D;&#x540C;&#x7684;&#x5730;&#x65B9;&#x91CD;&#x7528;&#x89C6;&#x56FE;&#x800C;&#x5F15;&#x5165;&#x7684;&#x6982;&#x5FF5;&#x3002;</p>
<p>&#x8FED;&#x4EE3;&#x662F;&#x51E0;&#x4E4E;&#x6240;&#x6709;&#x6A21;&#x7248;&#x8BED;&#x6CD5;&#x90FD;&#x4F1A;&#x5F15;&#x5165;&#x7684;&#x4E00;&#x4E2A;&#x4E1C;&#x897F;(&#x6211;&#x4E5F;&#x662F;&#x8FD9;&#x4E48;&#x505A;)&#x3002;&#x540C;&#x6837;&#xFF0C;&#x5BF9;&#x4E8E; JSX:&#x548C;&#x5176;&#x4ED6; javascript &#x7A0B;&#x5E8F;&#x4E00;&#x6837;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x9009;&#x62E9;&#x4E00;&#x79CD;&#x9002;&#x5408;&#x7684;&#x8FED;&#x4EE3;&#x65B9;&#x5F0F;&#xFF1A;[].forEach,[].map(),for &#x548C; while &#x5FAA;&#x73AF;&#x7B49;&#x7B49;&#x3002;</p>
<p>&#x903B;&#x8F91;&#x5C31;&#x548C;&#x8FED;&#x4EE3;&#x4E00;&#x6837;&#xFF0C;&#x6A21;&#x7248;&#x8BED;&#x6CD5;&#x90FD;&#x559C;&#x6B22;&#x91CD;&#x5B9A;&#x4E49;&#x3002;&#x4E00;&#x65B9;&#x9762;&#xFF0C;&#x65E0;&#x903B;&#x8F91;&#x6A21;&#x7248;&#x8BA9;&#x89C6;&#x56FE;&#x52A0;&#x5165;&#x903B;&#x8F91;&#x5F88;&#x4E0D;&#x65B9;&#x4FBF;&#xFF1A;&#x4E0D;&#x5408;&#x7406;&#x7684;&#x8BBE;&#x8BA1;&#xFF0C;&#x5982;<code>{{#if value}}</code>&#xFF0C;&#x628A;&#x903B;&#x8F91;&#x52A0;&#x5165;&#x5230; controller &#x5C42;&#xFF0C;&#x4F7F; controller &#x53D8;&#x7684;&#x5341;&#x5206;&#x81C3;&#x80BF;&#x3002;&#x8FD9;&#x79CD;&#x89C4;&#x907F;&#x65B9;&#x5F0F;&#xFF0C;&#x521B;&#x9020;&#x4E86;&#x4E00;&#x79CD;&#x63CF;&#x8FF0;&#x66F4;&#x590D;&#x6742;&#x903B;&#x8F91;&#x7684;&#x8BED;&#x8A00;&#xFF0C;&#x5BFC;&#x81F4;&#x4E86;&#x53EF;&#x9884;&#x6D4B;&#x964D;&#x4F4E;&#x548C;&#x5B89;&#x5168;&#x9690;&#x60A3;&#x3002;</p>
<p>&#x53E6;&#x4E00;&#x65B9;&#x9762;&#xFF0C;&#x7528;&#x4EE3;&#x7801;&#x751F;&#x6210;&#x6280;&#x672F;&#x7684;&#x5F15;&#x64CE;&#xFF08;&#x4E00;&#x79CD;&#x7B80;&#x964B;&#x5230;&#x4E0D;&#x53EF;&#x539F;&#x8C05;&#x7684;&#x6280;&#x672F;&#xFF09;&#xFF0C;&#x901A;&#x5E38;&#x81EA;&#x8BE9;&#x5177;&#x6709;&#x4F7F;&#x7528;&#x4EFB;&#x610F; JavaScript &#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x80FD;&#x529B;&#xFF0C;&#x6765;&#x6267;&#x884C;&#x903B;&#x8F91;&#x751A;&#x81F3;&#x8FED;&#x4EE3;&#x3002;&#x8FD9;&#x91CC;&#x6709;&#x4E2A;&#x6211;&#x4EEC;&#x4E3A;&#x4EC0;&#x4E48;&#x4E00;&#x5B9A;&#x8981;&#x907F;&#x514D;&#x4F7F;&#x7528;&#x8FD9;&#x9879;&#x6280;&#x672F;&#x7684;&#x539F;&#x56E0;&#xFF1A;&#x4F60;&#x7684;&#x4EE3;&#x7801;&#x5DF2;&#x7ECF;&#x8131;&#x79BB;&#x4E86;&#x539F;&#x6765;&#x7684;&#x2019;&#x4F4D;&#x7F6E;&#x2018;&#xFF08;&#x6A21;&#x5757;&#xFF0C;&#x95ED;&#x5305;&#x6216;&#x6807;&#x7B7E;&#x5185;&#xFF09;&#xFF0C;&#x5728;&#x522B;&#x5904;&#x6267;&#x884C;&#x3002;&#x8FD9;&#x5BF9;&#x6211;&#x800C;&#x8A00;&#xFF0C;&#x4E0D;&#x53EF;&#x9884;&#x6D4B;&#xFF0C;&#x5E76;&#x4E14;&#x4E0D;&#x5B89;&#x5168;&#x3002;</p>
<blockquote>
<p>JSX &#x62E5;&#x6709; javascript &#x7684;&#x4E00;&#x5207;&#x80FD;&#x529B;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x5728; build &#x9636;&#x6BB5;&#x751F;&#x6210;&#x602A;&#x5F02;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5E76;&#x4E14;&#x4E0D;&#x4F7F;&#x7528; eval()</p>
</blockquote>
<pre><code class="lang-javascript"><span class="hljs-comment">// Array of strings we want to show in a list:</span>
<span class="hljs-keyword">let</span> items = [<span class="hljs-string">&apos;foo&apos;</span>, <span class="hljs-string">&apos;bar&apos;</span>, <span class="hljs-string">&apos;baz&apos;</span>]

<span class="hljs-comment">// creates one list item given some text:</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">item</span>(<span class="hljs-params">text</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>{text}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span>
}

<span class="hljs-comment">// a &quot;view&quot; with &quot;iteration&quot; and &quot;a partial&quot;:</span>
<span class="hljs-keyword">let</span> list = render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>{items.map(item)}<span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>)
</code></pre>
<p>render()&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; DOM &#x8282;&#x70B9;&#xFF08;&#x4E0A;&#x9762;&#x662F;\<ul>&#xFF09;,&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x53EA;&#x8981;&#x628A;&#x5B83;&#x52A0;&#x5165; DOM &#x4E2D;&#xFF1A;</ul></p>
<pre><code class="lang-javascript"><span class="hljs-built_in">document</span>.body.appendChild(list)
</code></pre>
<h2 id="&#x5408;&#x5728;&#x4E00;&#x8D77;">&#x5408;&#x5728;&#x4E00;&#x8D77;</h2>
<p>&#x8FD9;&#x91CC;&#x662F;&#x6211;&#x4EEC;&#x865A;&#x62DF; DOM &#x6E32;&#x67D3;&#x7684;&#x5B8C;&#x6574;&#x7248;&#xFF0C;&#x4E0B;&#x9762;&#x7684;&#x662F; CodePen &#x4E0A;&#x6709;&#x6837;&#x5F0F;&#x7684;&#x7248;&#x672C;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> ITEMS = <span class="hljs-string">&apos;hello there people&apos;</span>.split(<span class="hljs-string">&apos; &apos;</span>)

<span class="hljs-comment">// turn an Array into list items:</span>
<span class="hljs-keyword">let</span> list = (items) =&gt; items.map((p) =&gt; <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span> {p} <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span></span>)

<span class="hljs-comment">// view with a call out (&quot;partial&quot;) to generate a list from an Array:</span>
<span class="hljs-keyword">let</span> vdom = (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&apos;foo&apos;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Look, a simple JSX DOM renderer!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>{list(ITEMS)}<span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
)

<span class="hljs-comment">// render() converts our &quot;virtual DOM&quot; (see below) to a real DOM tree:</span>
<span class="hljs-keyword">let</span> dom = render(vdom)

<span class="hljs-comment">// append the new nodes somewhere:</span>
<span class="hljs-built_in">document</span>.body.appendChild(dom)

<span class="hljs-comment">// Remember that &quot;virtual DOM&quot;? It&apos;s just JSON - each &quot;VNode&quot; is an object with 3 properties.</span>
<span class="hljs-keyword">let</span> json = <span class="hljs-built_in">JSON</span>.stringify(vdom, <span class="hljs-literal">null</span>, <span class="hljs-string">&apos;  &apos;</span>)

<span class="hljs-comment">// The whole process (JSX -&gt; VDOM -&gt; DOM) in one step:</span>
<span class="hljs-built_in">document</span>.body.appendChild(render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">pre</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&apos;vdom&apos;</span>&gt;</span>{json}<span class="hljs-tag">&lt;/<span class="hljs-name">pre</span>&gt;</span></span>))
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="创建你自己的vuejs/创建你自己的vuejs.html" class="navigation navigation-prev " aria-label="Previous page: 创建你自己的 vuejs">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Redux:从0创建Store.html" class="navigation navigation-next " aria-label="Next page: Redux:从 0 创建 Store">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"JSX 什么鬼(一起来写一个 JSX 渲染器)","level":"1.2.3","depth":2,"next":{"title":"Redux:从 0 创建 Store","level":"1.2.4","depth":2,"path":"前端框架/Redux:从0创建Store.md","ref":"./前端框架/Redux:从0创建Store.md","articles":[]},"previous":{"title":"创建你自己的 vuejs","level":"1.2.2","depth":2,"path":"前端框架/创建你自己的vuejs/创建你自己的vuejs.md","ref":"./前端框架/创建你自己的vuejs/创建你自己的vuejs.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"前端框架/JSXWTH.md","mtime":"2021-03-16T03:03:45.743Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-03-16T03:43:03.824Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

